<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/5
 * Time: 16:03
 */

namespace App\Http\Admin\Administration\Models;

use Framework\BaseClass\Http\Model;
use App\Eloquent\Oa\ImageText as EloquentOaImageText;


class NoticePolicy extends Model
{

    public function getNoticePolicyList($page, $pageSize, $searchField,$oa_company_ids)
    {
        $list = EloquentOaImageText::where($searchField)->whereIn('oa_company_id',$oa_company_ids);
        $count = $list->count();
        $list = $list->forPage($page, $pageSize)->orderBy('created_at','desc')->get();

        $data['list'] = $list;
        $data['pageCurrent'] = $page;
        $data['totalRow'] = $count;
        return $data;
    }

    public function addNoticePolicy($request){
        $noticePolicy = new EloquentOaImageText();
        $noticePolicy->title = $request['title'];
        $noticePolicy->content = htmlspecialchars_decode($request['content']);
        $noticePolicy->main_image_url = $request['main_image_url'];
        $noticePolicy->type = $request['type'];
        $noticePolicy->created_at = time();
        $noticePolicy->oa_company_id =$request['oa_company_id'] ;
        $result = $noticePolicy->save();
        if ($result) {
            return true;
        } else {
            return false;
        }
    }

    public function editNoticePolicy($request)
    {
        $lawPost = EloquentOaImageText::where('id', $request['id'])->first();
        if (!$lawPost) {
            return xThrow(ERR_API_NOTICE_POLICY_NOT_EXIST);
        }
        $input = [
            'title' => $request['title'],
            'content' => htmlspecialchars_decode($request['content']),
            'type' => $request['type'],
            'updated_at' => time(),
        ];
        if (isset($request['main_image_url'])) {
            $input['main_image_url'] = $request['main_image_url'];
        }
        $result = $lawPost->update($input);


        if ($result) {
            return true;
        } else {
            return false;
        }
    }


    public function getNoticePolicyInfo($id){
        $result = EloquentOaImageText::where('id', '=', $id)->first();
        if (is_null($result)) xThrow(ERR_API_NOTICE_POLICY_NOT_EXIST);
        return $result;
    }

    public function delNoticePolicy($id)
    {
        $role = EloquentOaImageText::find($id);
        if (!$role) {
            return xThrow(ERR_API_NOTICE_POLICY_NOT_EXIST);
        }
        return $role->delete();
    }

}